package com.weishuang.monitor.leader.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.weishuang.monitor.leader.entity.ServerCpuRealTime;
import com.weishuang.monitor.leader.mapper.ServerCpuRealTimeMapper;
import com.weishuang.monitor.leader.mapper.ServersBaseInfoMapper;
import com.weishuang.monitor.leader.service.IServerCpuRealTimeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务器CPU实时信息 服务实现类
 * </p>
 *
 * @author weishuang
 * @since 2021-03-23
 */
@Service
public class ServerCpuRealTimeServiceImpl extends ServiceImpl<ServerCpuRealTimeMapper, ServerCpuRealTime> implements IServerCpuRealTimeService {

    @Value("${leader.limitNum.cpu:100000}")
    private Long limitNumCpu;

    @Autowired
    private ServersBaseInfoMapper serversBaseInfoMapper;

    /**
     * 服务器CPU实时数据限制处理
     */
    @Override
    public void serverCpuDataLimit() {
        //查询所有状态正常，且在线的服务器列表
        List<String> serversId = serversBaseInfoMapper.listNormalAndOnlieServer();
        if (serversId != null && serversId.size() > 0) {
            for (String serverId : serversId) {
                baseMapper.deleteLimitNumData(serverId,limitNumCpu);
            }
        }
    }

    /**
     * 获取Cpu最新150条数据
     * @param sysId
     * @return
     */
    @Override
    public List<ServerCpuRealTime> listTop(String sysId) {
        LambdaQueryWrapper<ServerCpuRealTime> last = Wrappers.lambdaQuery(ServerCpuRealTime.class)
                .eq(ServerCpuRealTime::getSysId, sysId)
                .orderByDesc(ServerCpuRealTime::getCreateTime).last("limit 100").orderByAsc(ServerCpuRealTime::getCreateTime);
        return baseMapper.selectList(last);
    }

}
